package com.youzhi.backend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youzhi.backend.dto.LoginResponse;
import com.youzhi.backend.dto.Result;
import com.youzhi.backend.entity.User;
import com.youzhi.backend.request.*;
import com.youzhi.backend.service.IUserService;
import com.youzhi.backend.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 雇员表 前端控制器
 * </p>
 *
 * @author xumeng
 * @since 2022-11-18
 */
@RestController
@Api(tags = "用户")
@RequestMapping("/user")
public class UserController extends BaseController {


    @Autowired
    private IUserService service;


    @PostMapping("/add")
    @RequiresPermissions("user:add")
    @ApiOperation(value = "添加用户", notes = "添加用户接口")
    public Result addUser(@RequestBody UpdateUserRequest request) {
        verifyIfBlank(request, User.Fields.name, User.Fields.phone, User.Fields.sex);
        service.addUser(request);
        return Result.ok();
    }


    @PostMapping("/page")
    @RequiresUser
    @ApiOperation("分页查询用户")
    public Result userPage(@RequestBody UserPageRequest request) {
        IPage<User> userIPage = service.userPage(request);
        return Result.ok(userIPage);
    }


    @PostMapping("/login")
    @ApiOperation("登陆")
    public Result login(@RequestBody LoginRequest request, HttpServletRequest httpServletRequest) {
        verifyIfBlank(request, LoginRequest.Fields.name, LoginRequest.Fields.password);
        //绑定登陆客户端ip
        request.setClientIp(CommonUtil.getIPAddress(httpServletRequest));
        LoginResponse response = service.login(request);
        return Result.ok(response);
    }


    @RequiresUser
    @PostMapping("/logout")
    @ApiOperation("退出登陆")
    public Result logout(@RequestBody LogoutRequest request) {
        service.logout(request);
        return Result.ok();
    }

    @PostMapping("/update")
    @RequiresPermissions("user:edit")
    @ApiOperation("更新用户信息")
    public Result update(@RequestBody UpdateUserRequest request) {
        verifyIfBlank(request, UpdateUserRequest.Fields.id);
        service.updateUser(request);
        return Result.ok();
    }


    @PostMapping("/reset/password")
    @RequiresPermissions("user:resetPassword")
    @ApiOperation("重置密码")
    public Result resetPassword(@RequestBody UpdateUserRequest request) {
        verifyIfBlank(request, UpdateUserRequest.Fields.id);
        service.updateUser(request);
        return Result.ok();
    }


    @RequiresUser
    @PostMapping("/modify/password")
    @ApiOperation("修改密码")
    public Result modifyPassword(@RequestBody ModifyPasswordRequest request) {
        verifyIfBlank(request, ModifyPasswordRequest.Fields.oldPassword,
                ModifyPasswordRequest.Fields.newPassword,
                ModifyPasswordRequest.Fields.affirmedNewPassword);
        service.modifyPassword(request);
        return Result.ok();
    }

    @RequiresPermissions("user:delete")
    @PostMapping("/delete")
    @ApiOperation("删除用户")
    public Result delete(@RequestBody DeleteUserRequest request) {
        verifyListIsEmpty(request.getUserIds());
        service.deleteUsers(request.getUserIds());
        return Result.ok();
    }


    @ApiOperation("根据用户名模糊查询用户信息(用于输入建议)")
    @PostMapping("/search/name")
    public Result queryName(@RequestBody UpdateUserRequest updateUserRequest) {
        verifyIfBlank(updateUserRequest, UserPageRequest.Fields.name);
        List<Map<String, Object>> list = service.queryUserName(updateUserRequest.getName());
        return Result.ok(list);
    }


    @ApiOperation("移除用户组织部门信息")
    @PostMapping("/remove/organization/id")
    public Result removeUserOrganization(@RequestBody UpdateUserRequest request) {
        verifyIfBlank(request, UpdateUserRequest.Fields.id);
        service.removeUserOrganization(request.getId());
        return Result.ok();
    }


    @ApiOperation("将用户加入部门组织")
    @PostMapping("/add/organization")
    public Result addUserOrganization(@RequestBody UpdateUserRequest request) {
        verifyIfBlank(request, UpdateUserRequest.Fields.organizationId, UpdateUserRequest.Fields.name);
        service.addUserOrganization(request.getOrganizationId(), request.getName());
        return Result.ok();
    }


}

